package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.mapper.WsdStudentsMapper;
import com.ruoyi.business.service.IWsdParentsService;
import com.ruoyi.business.service.IWsdStudentsService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdStudents;
import com.ruoyi.common.core.domain.vo.studentInformationVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class WsdStudentsServiceImpl extends ServiceImpl<WsdStudentsMapper, WsdStudents> implements IWsdStudentsService
{
    @Autowired
    private WsdStudentsMapper wsdStudentsMapper;
    @Autowired
    private IWsdParentsService wsdParentsService;
    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public WsdStudents selectWsdStudentsById(Long id)
    {
        return wsdStudentsMapper.selectWsdStudentsById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param wsdStudents 学生信息
     * @return 学生信息
     */
    @Override
    public List<WsdStudents> selectWsdStudentsList(WsdStudents wsdStudents) {
        return wsdStudentsMapper.selectWsdStudentsList(wsdStudents);
    }

    /**
     * 新增学生信息
     *
     * @param wsdStudents 学生信息
     * @return 结果
     */
    @Override
    public int insertWsdStudents(WsdStudents wsdStudents)
    {
        wsdStudents.setCreateTime(DateUtils.getNowDate());
        //return wsdStudentsMapper.insertWsdStudents(wsdStudents);
        return wsdStudentsMapper.insert(wsdStudents);
    }
    @Override
    public int insertWsdStudents1(WsdStudents wsdStudents)
    {
        wsdStudents.setCreateTime(DateUtils.getNowDate());
        //return wsdStudentsMapper.insertWsdStudents(wsdStudents);
        try {
            wsdStudentsMapper.insert(wsdStudents);
        }catch (Exception e){
            return 0;
        }
        SysUser sysUser = new SysUser();
        sysUser.setStudentId( wsdStudents.getId() );
        sysUser.setNickName( "家长1" );
        sysUser.setPhonenumber( wsdStudents.getParentsPhone() );
        sysUser.setUserName( wsdStudents.getParentsPhone() );
        sysUser.setSchoolId(wsdStudents.getSchoolId());
        //小程序用户11
        sysUser.setUserType( "11" );
        return wsdParentsService.insertWsdParents(sysUser);
    }

    /**
     * 修改学生信息
     *
     * @param wsdStudents 学生信息
     * @return 结果
     */
    @Override
    public int updateWsdStudents(WsdStudents wsdStudents)
    {
        wsdStudents.setUpdateTime(DateUtils.getNowDate());
        return wsdStudentsMapper.updateWsdStudents(wsdStudents);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdStudentsByIds(Long[] ids)
    {
        return wsdStudentsMapper.deleteWsdStudentsByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdStudentsById(Long id)
    {
        return wsdStudentsMapper.deleteWsdStudentsById(id);
    }

    /**
     * 获得学生的信息
     *
     * @param schoolId
     * @param phonenumber
     * @return
     */
    @Override
    public List<studentInformationVO> studentInformation(Long schoolId, Long studentId) {
        List<studentInformationVO> voList =  wsdStudentsMapper.studentInformation(schoolId,studentId);
        return voList;
    }

    /**
     * 获取学校人数
     *
     * @param classIds
     * @return
     */
    @Override
    public Long populationStatistics(Long[] classIds) {
        LambdaQueryWrapper<WsdStudents> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(WsdStudents::getClassId,classIds);
        wrapper.eq(WsdStudents::getIsDeleted, Constants.STATUSONE);
        return this.count(wrapper);
    }
}
